package com.hntxk.cqzd.diacrisis.service.impl;

import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.common.util.CalculateAge;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisAssay;
import com.hntxk.cqzd.diacrisis.model.DiacrisisAllModel;
import com.hntxk.cqzd.diacrisis.service.DiacrisisAssayService;
import com.hntxk.sp.dict.manager.DictManager;
import com.hntxk.sp.dict.model.Dict;
import com.hntxk.sp.dict.service.IDictService;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("diacAssayService")
@Transactional(rollbackFor = Exception.class)
public class DiacrisisAssayServiceImpl extends BaseServiceImpl<TblDiacrisisAssay> implements DiacrisisAssayService {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.diacrisis.mapper.DiacrisisAssayMapper.";

    public DiacrisisAssayServiceImpl() {
        super(MAPPER_NAMESPACE);
    }


    @Override
    public ResultModel<DiacrisisAllModel> insert(DiacrisisAllModel diacrisisAllModel) {

        //获取model中的Assay数据  设置主键值和外键值
        for(TblDiacrisisAssay assay:diacrisisAllModel.getDiacrisisAssay()){
                setSelectName(assay); //设置下拉值
                getPrimaryKey(assay);   //设置主键
                assay.setDiacrisisId(diacrisisAllModel.getTblDiacrisisInfo().getDiacrisisId());
             insert(MAPPER_NAMESPACE + "insert", assay);


            LogRecordUtil.addLog(new ActionLog(TblDiacrisisAssay.class, assay.getAssayId() + "", "12",
                    ActionType.ADD, "新增检验信息：" + assay.getAntibodyNatureName() + "[ " + assay.getNiptTypeName() + " ]"));

        }
        return ResultModel.empty();
    }



    @Override
    public ResultModel<DiacrisisAllModel> update(DiacrisisAllModel diacrisisAllModel) {

        //获取外键id
        Long diacrisisId = diacrisisAllModel.getTblDiacrisisInfo().getDiacrisisId();
        ResultModel<DiacrisisAllModel> resultModel=null;
        //根据外键查询库中是否有值
        List<TblDiacrisisAssay> assays = getByFkId(diacrisisId);
        if(assays!=null&&assays.size()>0){
            //根据外键删除此表的数据
           delete(MAPPER_NAMESPACE+"deleteByFkId",diacrisisId);

        }
        //插入页面修改的数据
        return insert(diacrisisAllModel);


    }

    @Override
    public TblDiacrisisAssay getById(Long id) {
        return null;
    }

    @Override
    public List<TblDiacrisisAssay> getByFkId(Long id) {

        TblDiacrisisAssay tblDiacrisisAssay=new TblDiacrisisAssay();
        tblDiacrisisAssay.setDiacrisisId(id);
        List<TblDiacrisisAssay> list = (List<TblDiacrisisAssay>) query(MAPPER_NAMESPACE + "getByFkId", tblDiacrisisAssay);
        return list;
    }

    /**
     * 新增修改时设置主键和下拉选项的name值
     * @param assay
     */
    public void setSelectName(TblDiacrisisAssay assay){
        if(assay.getNiptTypeId()!=null){
            //设置nipt下拉name
            Dict niptTypeDict = DictManager.getInstance().getDict("CQ_BASE_NIPT_TYPE", assay.getNiptTypeId());
            assay.setNiptTypeName(niptTypeDict.getText());
        }

        if(assay.getTangScreenId()!=null){
            //设置唐筛下拉name
            Dict tangScreenDict = DictManager.getInstance().getDict("CQ_BASE_TANG_SCREEN", assay.getTangScreenId());
            assay.setTangScreenName(tangScreenDict.getText());
        }

        if(assay.getBloodTypeId()!=null){
            //设置血型下拉name
            Dict bloodTypeDict = DictManager.getInstance().getDict("CQ_BASE_BLOOD_TYPE", assay.getBloodTypeId());
            assay.setBloodTypeName(bloodTypeDict.getText());
        }

        if(assay.getBtNatureId()!=null){
            //设置血型性级下拉name
            Dict btNatureDict = DictManager.getInstance().getDict("CQ_BASE_BT_NATURE", assay.getBtNatureId());
            assay.setBtNatureName(btNatureDict.getText());
        }

        String infectionScreenId = assay.getInfectionScreenId();
        if(StringUtil.isNotNull(infectionScreenId)){
            CalculateAge.idToName(infectionScreenId,"CQ_BASE_INFECTION_SCREEN");
        }

        if(assay.getAntibodyNatureId()!=null){
            //设置不规则抗体下拉name
            Dict antibodyNature = DictManager.getInstance().getDict("CQ_BASE_ANTIBODY_NATURE", assay.getAntibodyNatureId());
            assay.setAntibodyNatureName(antibodyNature.getText());
        }



    }

}
